Precinct level results for MN CD5

Laste updated: 9am, November 6, 2020

---
title: "MN CD5 General Presidential Election Results 2020"
output: 
  flexdashboard::flex_dashboard:
    source_code: embed
---


```{r, include=FALSE}
library(ggmap)
library(OpenStreetMap)
library(tidyverse)
library(geojsonio)
library(sf)
library(htmltools)
library(leaflet)



```



Precinct level results for MN CD5 {data-icon="fa-list"}
===================================== 

### Laste updated: 9am, November 6, 2020


```{r}


# Load the precinct shapefile
mn5 <- read_sf("mpls_voting/mn-cd5-precincts.json") %>%
  mutate(Precinct = toupper(Precinct)) 


# Load the precint label info
precinct_lookup <- read_delim("mpls_voting/mn_precinct_general_president_2020_NAMES.txt", delim=";",
           col_names = c("County ID", "Precinct ID", "Precinct Name",
                         "Congressional District", "Legislative District",
                         "County Commissioner District","Judicial District",
                         "Soil and Water Conservation District", 
                         "MCD FIPS code", "School District number")) %>%
  select(`County ID`, `Precinct ID`, `Precinct Name`)


# Load the voting results
precinct_data_names <- c("State", "County ID", "Precinct ID", "Office ID",
                         "Office Name", "District", 
                         "Candidate Order Code", 
                         "Candidate Name",
                         "Suffix",
                         "Incumbent Code",
                         "Party Abbreviation",
                         "Number of Precincts reporting",
                         "Total number of precincts voting for the office",
                         "Votes for Candidate",
                         "Percentage of Votes for Candidate out of Total Votes for Office",
                         "Total number of votes for Office in area")


data_mn_general_2020 <- read_delim("mpls_voting/mn_precinct_general_president_2020.txt", delim=";",
                                   col_names = precinct_data_names) 


# Join the voting data with the shapefile
mn_cd5 <- left_join(data_mn_general_2020, precinct_lookup) %>% 
  select(State, `County ID`, `Precinct ID`, 
         Precinct=`Precinct Name`,
         `Candidate Name`, votes=`Votes for Candidate`, total_votes=`Total number of votes for Office in area`) %>%
  filter(str_detect(tolower(`Candidate Name`), "donald|joseph")) %>%
  mutate(candidate = ifelse(str_detect(`Candidate Name`, "Trump"), "trump", "biden"),
         prop = 100 * votes / total_votes) %>%
  select(-`Candidate Name`) %>%
  pivot_wider(names_from = candidate, values_from=c("votes", prop)) %>%
  mutate(prop_diff = prop_biden - prop_trump) %>%
  arrange(Precinct) %>%
  mutate(Precinct = str_replace(Precinct, "P-0", "P-")) %>%
  left_join(mn5, .) %>%
  mutate(biden_cat = cut(prop_biden, c(0, 70, 80, 90, 100))) 


# Set a color palette
pal <- colorFactor(
  palette = c('red', '#6baed6', '#4292c6', '#2171b5', '#08306b'),
  #palette = 'Blues',   # RColorBrewer::display.brewer.all()
  domain = mn_cd5$biden_cat
)


# Plot
leaflet(mn_cd5) %>% 
  addTiles()  %>%
  addPolygons(fillColor = ~pal(biden_cat), stroke = TRUE, color="black", weight=1,
              popup = paste0("",mn_cd5$Precinct,"
", "Total votes cast: ", mn_cd5$total_votes,"
", "% votes for Trump/Pence: ",paste0(round(mn_cd5$prop_trump,2),"%")," (n=",mn_cd5$votes_trump,")
", "% votes for Biden/Harris: ",paste0(round(mn_cd5$prop_biden,2),"%")," (n=",mn_cd5$votes_biden,")
")) ```